package com.zhiyin.seal.controller;

import com.google.common.collect.Lists;
import com.zhiyin.seal.entity.bo.ElectronicFenceBo;
import com.zhiyin.seal.entity.bo.ElectronicFenceDetailBo;
import com.zhiyin.seal.entity.bo.SealBo;
import com.zhiyin.seal.entity.dto.ElectronicFenceDTO;
import com.zhiyin.seal.entity.dto.ResponseDTO;
import com.zhiyin.seal.entity.vo.ElectronicFenceVo;
import com.zhiyin.seal.entity.vo.SealVo;
import com.zhiyin.seal.service.ElectronicFenceService;
import com.zhiyin.seal.service.SealService;
import com.zhiyin.seal.util.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 电子围栏控制器
 */
@RestController
@RequestMapping("/electronicfence")
public class ElectronicFenceController {

    @Autowired
    ElectronicFenceService electronicFenceService;

    @Autowired
    SealService sealService;

    @PostMapping("/add")
    public ResponseDTO<Boolean> addElectronicFence(@RequestBody ElectronicFenceDTO electronicFenceDTO){
        ElectronicFenceBo electronicFenceBo = BeanUtil.copy(electronicFenceDTO,ElectronicFenceBo.class);
        // 添加电子围栏返回数据id
        Long elecId = electronicFenceService.addElectronicFence(electronicFenceBo);
        // 插入关联印章
        return ResponseDTO.succData(electronicFenceService.addElectronicFenceDetail(elecId,electronicFenceDTO.getSealIdList()));
    }

    @GetMapping("/list")
    public ResponseDTO<List<ElectronicFenceVo>> getElectronicFenceList(@RequestParam Long companyId){
        List<ElectronicFenceBo> electronicFenceBoList = electronicFenceService.getElectronicFenceList(companyId);
        List<ElectronicFenceVo> electronicFenceVoList = BeanUtil.copyList(electronicFenceBoList,ElectronicFenceVo.class);
        if(electronicFenceVoList != null){
            for (ElectronicFenceVo electronicFenceVo : electronicFenceVoList) {
                List<SealBo> sealBoList = getElectroniFenceSealList(electronicFenceVo.getId());
                electronicFenceVo.setSealVoList(BeanUtil.copyList(sealBoList,SealVo.class));
            }
        }
        return ResponseDTO.succData(electronicFenceVoList);
    }

    @GetMapping("/list/by/seal")
    public ResponseDTO<List<ElectronicFenceVo>> getElectronicFenceListBySeal(@RequestParam Long sealId){
        return ResponseDTO.succData(BeanUtil.copyList(electronicFenceService.getElectronicFenceListBySeal(sealId),ElectronicFenceVo.class));
    }

    @GetMapping("/detail")
    public ResponseDTO<List<SealVo>> getElectronicFenceDetail(@RequestParam Long electronicFenceId){
        List<SealBo> sealBoList = getElectroniFenceSealList(electronicFenceId);
        return ResponseDTO.succData(BeanUtil.copyList(sealBoList, SealVo.class));
    }

    @GetMapping("/delete")
    public ResponseDTO<Boolean> deleteElectronicFence(@RequestParam Long electronicFenceId){
        return ResponseDTO.succData(electronicFenceService.removeElectronicFenceById(electronicFenceId));
    }

    List<SealBo> getElectroniFenceSealList(Long electronicFenceId){
        ElectronicFenceBo electronicFenceBo = electronicFenceService.getElectronicFenceById(electronicFenceId);
        if(electronicFenceBo == null){
            return null;
        }
        List<ElectronicFenceDetailBo> electronicFenceDetailBoList = electronicFenceService.getElectronicFenceDetail(electronicFenceId);
        List<Long> sealIds = Lists.newArrayList();
        sealService.buildSealIdsByElectronicFenceList(electronicFenceDetailBoList,sealIds);
        return sealService.getSealListByIdsAndCompany(sealIds,electronicFenceBo.getCompanyId());
    }
}
